package com.erp.framework.controller.taglib;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

import com.erp.app.common.ProjectConstants;
import com.erp.framework.controller.form.SimpleCoreForm;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;
import com.erp.framework.util.mainmenu.MenuObject;

//import javax.servlet.jsp.tagext.SimpleTagSupport;

//import com.apexpro.appObject.ejb.beans.MenuObject;
//import com.apexpro.object.common.util.CommonUtil;
//import com.apexpro.object.web.form.BaseForm;
//import com.apexpro.object.web.form.UserForm;

public class CustomAccessPanelTag extends TagSupport {
    private String form;
    private String styleClass;

    public String getForm() {
        return form;
    }

    public void setForm(String form) {
        this.form = form;
    }

    public String getStyleClass() {
        return styleClass;
    }

    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }

    public int doEndTag() throws JspException {
        PageContext context = this.pageContext;
        JspWriter out = context.getOut();

        try {
            if (form == null || form.length() == 0)
                throw new JspException("Form name is unspecified.");

            SimpleCoreForm user_form = (SimpleCoreForm) context.getAttribute(form, PageContext.SESSION_SCOPE);

            Collection list = user_form.getMenuObjectList();

            for (Iterator it = list.iterator(); it.hasNext();) {
                MenuObject menu_obj = (MenuObject) it.next();

                renderAccessElement(menu_obj);
            }
        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }

        return EVAL_PAGE;
    }

    private void renderAccessElement(MenuObject menu_obj) throws IOException {
        PageContext context = this.pageContext;
        JspWriter out = context.getOut();

        boolean isEnable = ProjectConstants.GENERIC_YES.equals(menu_obj.getEnableDisplay());
        boolean isVisible = ProjectConstants.GENERIC_YES.equals(menu_obj.getVisibleDisplay());
        String obj_type = menu_obj.getObjectType();

        if (!isVisible || obj_type == null || obj_type.length() == 0)
            return;

        // Right now we only support button element
        if ("button".equals(obj_type.toLowerCase())) {
            out.print("<input type=\"button\"");
        }

        if (!isEnable)
            out.print(" disabled");

        out.print(" name=\"" + CommonUtil.nullToString(menu_obj.getObjectId()) + "\"");

        out.print(" value=\"" + CommonUtil.nullToString(menu_obj.getName()) + "\"");

        out.print(" onclick=\"doSubmit('" + CommonUtil.nullToString(menu_obj.getObjectAction()) + "')\"");

        if (styleClass != null && styleClass.length() > 0)
            out.print(" class=\"" + styleClass + "\"");

        out.print(">");
    }
}
